package com.example.petstoreservice.controller;

import com.example.petstoreservice.entity.Product;
import com.example.petstoreservice.entity.Result;
import com.example.petstoreservice.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    @GetMapping("/all")
    public Result<List<Product>> getAll() {
        Result<List<Product>> res = new Result<>();
        res.setData(productService.getAll());
        return res;
    }

    @PostMapping("/insertGood")
    public Result<?> insertGood(@RequestBody Product product) {
        if (productService.insertGood(product)) {
            return Result.success("添加成功");
        } else {
            return Result.fail("新增失败!");
        }
    }
    @GetMapping("/getByShopId/{id}")
    public Result<?> getByShopId(@PathVariable Long id){
        Result<List<Product>> res = new Result<>();
        List<Product> product = productService.getByShopId(id);
        if(product != null)
        {
            res.setData(product);
            res.setStatusCode("200");
            return res;
        }else{
            return Result.fail("未查询到当前商品");
        }
    }
    @GetMapping("/getCat")
    public Result<List<Product>> getCat(){
        Result<List<Product>> res = new Result<>();
        List<Product> product = productService.getCat();
        if(product != null)
        {
            res.setData(product);
            res.setStatusCode("200");
            return res;
        }else{
            return Result.fail("未查询到当前商品");
        }
    }
    @GetMapping("/getDog")
    public Result<List<Product>> getDog(){
        Result<List<Product>> res = new Result<>();
        List<Product> product = productService.getDog();
        if(product != null)
        {
            res.setData(product);
            res.setStatusCode("200");
            return res;
        }else{
            return Result.fail("未查询到当前商品");
        }
    }


    @PostMapping("/updateGood")
    public Result<?> updateGood(@RequestBody Product product) {
        Long id = product.getProductId();
        Product p = productService.getById(id);
        if (p == null) {
            return Result.fail("当前商品不存在");
        }
        if (product.getProductName() != null) {
            p.setProductName(product.getProductName());
        }
        if (product.getDescription() != null) {
            p.setDescription(product.getDescription());
        }
        if (product.getCategoryId() != null) {
            p.setCategoryId(product.getCategoryId());
        }
        if (product.getPrice() != null) {
            p.setPrice(product.getPrice());
        }
        if (product.getStock() != null) {
            p.setStock(product.getStock());
        }
        if (product.getSupplierId() != null) {
            p.setSupplierId(product.getSupplierId());
        }
        if (product.getImageUrl() != null) {
            p.setImageUrl(product.getImageUrl());
        }
        if (product.getVideoUrl() != null) {
            p.setVideoUrl(product.getVideoUrl());
        }
        if (product.getCreateTime() != null) {
            p.setCreateTime(product.getCreateTime());
        }
        if (product.getUpdateTime() != null) {
            p.setUpdateTime(product.getUpdateTime());
        }
        if (product.getSold() != null) {
            p.setSold(product.getSold());
        }
        if (product.getType() != null) {
            p.setType(product.getType());
        }
        if (productService.updateGood(p)) {
            return Result.success("更新成功");
        } else {
            return Result.fail("更新失败");
        }
    }

    @PostMapping("/delete")
    public Result<?> deleteGood(@RequestParam Long id) {
        if (productService.delete(id)) {
            return Result.success("删除成功");
        } else {
            return Result.fail("删除失败");
        }
    }

    @GetMapping("/search/{keyword}")
    public Result<List<Product>> search(@PathVariable String keyword) {
        Result<List<Product>> res = new Result<>();
        res.setData(productService.search(keyword));
        return res;
    }
    @GetMapping("/findTop3/{id}")
    public Result<List<Product>> findTop3(@PathVariable Long id) {
        Result<List<Product>> res = new Result<>();
        res.setData(productService.findTop3Good(id));
        return res;
    }

    @GetMapping("/{id}")
    public Result<Product> getById(@PathVariable Long id) {
        Result<Product> res = new Result<>();
        res.setData(productService.getById(id));
        return res;
    }
    @GetMapping("/all-sorted")
    public Result<List<Product>> getAllSortedByClick() {
        Result<List<Product>> res = new Result<>();
        res.setData(productService.getAllSortedByClick());
        return res;
    }

    @PostMapping("/increment-click/{id}")
    public Result<?> incrementClick(@PathVariable Long id) {
        Product updatedProduct = productService.incrementClickAndGet(id);
        if (updatedProduct != null) {
            Result<Product> result = new Result<>();
            result.setData(updatedProduct);
            result.setStatusCode("200");
            result.setMessage("Click count incremented successfully");
            return result;
        } else {
            return Result.fail("Failed to increment click count");
        }
    }
}
